import { createStore } from "vuex";
import MAIN_STORE from '@/store/store.js'
import Api from "@/api";
const Store = createStore({
  state: {
    cesiumSceneIsCreated: false,
    // 时间轴数据
    dataOfYear: [],
    // 底图的默认 ID
    baseMapActiveId: 'base-yaogan',
    originView: [116.76518883912635, 36.18327910283026],
    globalLoading: false
  },
  getters: {
  },
  mutations: {
    SET_GLOBAL_LOADING(state, { tag }) {
      state.globalLoading = tag
    },
    SET_CESIUM_SCENE_CREATED(state) {
      state.cesiumSceneIsCreated = true;
    },
    SET_BASE_MAP_ACTIVE_ID(state, { id }) {
      state.baseMapActiveId = id;
    },
    LOCATION_CERTAIN_VIEW(state, { position, accu }) {
      const mapIns = MAIN_STORE.GLOBAL_MAP_INS
      if (!mapIns) { return }
      const obj = {
        center: position
      }
      if (accu) {
        obj.zoom = mapIns.getView().getMaxZoom()
      }
      mapIns.getView().animate(obj)
    },
    LOCATION_ORIGIN_VIEW(state) {
      const mapIns = MAIN_STORE.GLOBAL_MAP_INS
      if (!mapIns) { return }
      mapIns.getView().animate({
        zoom: 18,
        center: state.originView
      })
    },
    DOWNLOAD_EXCELL_DATA(state, { data, title }) {
      const jsonData = data;
      // 创建一个工作簿
      const workbook = XLSX.utils.book_new();
      // 将JSON数据转换为工作表
      const worksheet = XLSX.utils.json_to_sheet(jsonData);
      // 将工作表添加到工作簿
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
      // 生成Excel文件的二进制流
      const excelBuffer = XLSX.write(workbook, {
        bookType: 'xlsx',
        type: 'array'
      });
      // 创建Blob对象
      const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
      // 创建下载链接
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = title + '.xlsx';
      // 触发下载
      document.body.appendChild(a);
      a.click();
      // 清理
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    }
  },
});
export default Store;
